package com.gable.exhibition.service.impl;

import com.gable.common.util.ReturnResultUtil;
import com.gable.common.util.UUIDGenerator;
import com.gable.exhibition.dao.SupplierDao;
import com.gable.exhibition.service.SupplierService;
import com.gable.exhibition.util.MessageUtil;
import com.gable.exhibition.view.SupplierView;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Lesson on 2018/3/16.
 */
@Service("supplierService")
class SupplierServiceImpl implements SupplierService {

    Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    SupplierDao supplierDao;

    @Override
    public ReturnResultUtil querySupplierList(SupplierView supplierView) {
        int count  = supplierDao.querySupplierCount(supplierView);
        List<SupplierView> list = supplierDao.querySupplierList(supplierView);
        SupplierView supplierView1 = new SupplierView();
        supplierView1.setCount(count);
        supplierView1.setList(list);
        return new ReturnResultUtil(200, MessageUtil.QUERYSUCCESS,supplierView1);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ReturnResultUtil addOrUpdateSupplier(SupplierView supplierView) {
        int count = supplierDao.checkSupplierNoIsExist(supplierView);
        if(count > 0){
            return new ReturnResultUtil(500,MessageUtil.SUPPLIERNOISEXIST);
        }
        int result = 0;
        if(supplierView.getSupplierId() == null){
            supplierView.setSupplierId(UUIDGenerator.getUniqueLong());
            result = supplierDao.insertSelective(supplierView);
        }else{
            result = supplierDao.updateByPrimaryKeySelective(supplierView);
        }

        if(result > 0){
            return new ReturnResultUtil(200,MessageUtil.SAVESUCCESS);
        }
        return new ReturnResultUtil(500,MessageUtil.SAVEFAIL);
    }

    @Override
    public ReturnResultUtil querySupplierInfo(Long supplierId) {
        SupplierView supplierView = supplierDao.querySupplierInfo(supplierId);
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,supplierView);
    }

    @Override
    public ReturnResultUtil querySupplierListByManuId(Long manufacturerId) {
        List<SupplierView> list = supplierDao.querySupplierListByManuId(manufacturerId);
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,list);
    }
}
